package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.utils.DateUtil;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.L;

import java.util.List;

/**
 * @date on 2018/6/18 16:29
 * @email 909732184@qq.com
 * 任务-考试的列表适配器
 */
public class TaskExaminationAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private OnItemClickListener onItemClickListener;
    private List<ExamBean> data;
    private Drawable stateBg1,stateBg2;
    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<ExamBean> data) {
        this.data = data;
    }

    public List<ExamBean> getData() {
        return data;
    }

    public interface OnItemClickListener{
        void onExaminationItemClick(int position);
    }
    public TaskExaminationAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        stateBg1 = ContextCompat.getDrawable(context,R.drawable.left_label_black_bg);
        stateBg2 = ContextCompat.getDrawable(context,R.drawable.left_label_red_bg);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.main_task_examination_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;
            ExamBean bean = data.get(position);
            L.d(bean.getFaceUri());
            ImageLoadUtils.ImageLoad(context,bean.getFaceUri(),viewHolder.ivIcon,-1);
            viewHolder.tvTitle.setText(bean.getTitle()+"");
            viewHolder.tvType.setText("考试类型："+(bean.getLargeTypeLabel()==null?"暂无数据":bean.getLargeTypeLabel())+"");
            viewHolder.tvDate.setText("考试时间："+bean.getStartDate()+"始");
            String examIdFinish = data.get(position).getExamIdFinish();
            String examId = data.get(position).getExamId();
            //已经考试完毕
            if(examIdFinish!=null&&examIdFinish.length()>0){
                viewHolder.ivStatus.setVisibility(View.VISIBLE);
                viewHolder.tvGoExamination.setVisibility(View.GONE);
            }
            //正在考试
            else if(examId!=null&&examId.length() > 0 && (examIdFinish==null||examIdFinish.length()==0)){
                viewHolder.ivStatus.setVisibility(View.GONE);
                viewHolder.tvGoExamination.setVisibility(View.VISIBLE);
            }else {
                viewHolder.ivStatus.setVisibility(View.GONE);
                viewHolder.tvGoExamination.setVisibility(View.VISIBLE);
            }
            if(isPassline(data.get(position))){
                viewHolder.ivStatus.setImageResource(R.mipmap.icon_pass);
            }else {
                viewHolder.ivStatus.setImageResource(R.mipmap.icon_fail);
            }

            setDateState(viewHolder,data.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private TextView tvGoExamination;
        private TextView tvTitle;
        private TextView tvType;
        private TextView tvDate;
        private TextView tvState;
        private ImageView ivStatus;
        private ImageView ivIcon;
        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvType = itemView.findViewById(R.id.tvType);
            tvState = itemView.findViewById(R.id.tvState);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvGoExamination = itemView.findViewById(R.id.tvGoExamination);
            ivStatus = itemView.findViewById(R.id.ivStatus);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onExaminationItemClick(position);
                    }
                }
            });
        }
    }
    //是否及格
    private boolean isPassline(ExamBean bean){
        boolean isPass = false;
        try {
            //及格线
            int nmPassLine = Integer.parseInt(bean.getPassline());
            //个人平均分
            int nmAvgScore = Integer.parseInt(bean.getAvgScore());
            if(nmAvgScore>=nmPassLine){
                return true;
            }
        }catch (Exception e){
            L.e(e.toString());
        }
        return isPass;
    }

    //设置该考试目前的时间状态 未开始 已开始 已结束
    private void setDateState(ViewHolder viewHolder,ExamBean bean){
        String stStartDate = bean.getStartDate();
        String stEndDate = bean.getEndDate();
        DateUtil.DatePattern pattern = DateUtil.DatePattern.ALL_TIME;
        try {
            long nmStartDate = DateUtil.stringToDate(stStartDate,pattern).getTime();
            long nmEndDate = DateUtil.stringToDate(stEndDate,pattern).getTime();
            long nmTodayDate = DateUtil.stringToDate(DateUtil.getNowDate(pattern),pattern).getTime();
            //未开始
            if(nmTodayDate<nmStartDate&&nmTodayDate<nmEndDate){
                viewHolder.tvState.setText("未开始");
                viewHolder.tvState.setBackground(stateBg2);
            }else if(nmTodayDate>=nmStartDate&&nmTodayDate<nmEndDate){
                viewHolder.tvState.setText("考试中");
                viewHolder.tvState.setBackground(stateBg2);
            }else {
                viewHolder.tvState.setText("已结束");
                viewHolder.tvState.setBackground(stateBg1);
            }
            return;
        }catch (Exception e){
            L.e(e.toString());
        }
        viewHolder.tvState.setText("未开始");
        viewHolder.tvState.setBackground(stateBg2);
    }
}
